package redis

import (
	"time"

	"encoding/json"
	"goweb/ext/settings"
	"goweb/utils/commons"
)

func SetValue(key string, data interface{}, t int, db int) error {
	conn := conf.RedisConn(db)
	defer conn.Close()

	value, err := json.Marshal(data)
	if !commons.CheckErr(err) {
		return err
	}
	set := conn.Set(key, value, time.Duration(t)*time.Second)
	conf.Logger.Info("redis", string(set.Val()))
	return nil
}

func Exists(key string, db int) bool {
	conn := conf.RedisConn(db)

	defer conn.Close()
	_, err := conn.Get(key).Result()
	//exists, err := redis.Bool(conn.Do("EXISTS", key))
	if !commons.CheckErr(err) {
		return false
	}

	return true
}

func GetValue(key string, db int) (string, error) {
	conn := conf.RedisConn(db)
	defer conn.Close()

	reply, err := conn.Get(key).Result()
	if !commons.CheckErr(err) {
		return "", err
	}

	return reply, nil
}

func Delete(key string) {
	conn := conf.RedisConn(conf.RDb0)
	defer conn.Close()
	conn.Expire(key, 0)
}

//func GetAllValue(db int) map[string]string {
//	conn := conf.RedisConn(db)
//	defer conn.Close()
//	scan := conn.Scan(0, "", 100).String()
//}
